from django.db import models
from users.models import UserProfile,Grade
from course.models import Course,Paper,PaperList
from datetime import datetime
from datetime import timedelta


class CourseComments(models.Model):
    user = models.ForeignKey(UserProfile,verbose_name="用户",on_delete=models.CASCADE)
    course = models.ForeignKey(Course,verbose_name="课程",on_delete=models.CASCADE,help_text="课程")
    comments = models.CharField('评论',max_length=200,help_text="评论内容")
    add_time = models.DateTimeField('添加时间', default=datetime.now)

    class Meta:
        verbose_name = '课程评论管理'
        verbose_name_plural = verbose_name

    def __repr__(self):
        return self.comments

    __str__ = __repr__


class SensitiveWord(models.Model):
    word = models.CharField(max_length=32,verbose_name="敏感词")
    add_time = models.DateTimeField('添加时间', default=datetime.now)

    class Meta:
        verbose_name = '敏感词管理'
        verbose_name_plural = verbose_name
# Create your models here.


class UserAnswerLog(models.Model):
    user = models.ForeignKey(UserProfile, verbose_name=u"用户")
    course = models.ForeignKey(Course, verbose_name=u"课程")
    paper = models.ForeignKey(Paper,verbose_name=u"试卷")
    answer = models.TextField(verbose_name=u"用户答案")
    score = models.IntegerField(verbose_name=u"得分")
    add_time = models.DateField(default=datetime.now, verbose_name=u"作答时间")

    class Meta:
        verbose_name = u"用户做题记录管理"
        verbose_name_plural = verbose_name

    def __repr__(self):
        return "({0}) score={1}".format(self.user.username,self.score)


class UserScore(models.Model):
    user = models.ForeignKey(UserProfile, verbose_name=u"用户")
    paper = models.ForeignKey(PaperList, verbose_name=u"试卷列表")
    total = models.IntegerField(verbose_name=u"总分", default=0) # 用户得分
    add_time = models.DateField(verbose_name=u"录入时间",default=datetime.now)
    sumcore = models.IntegerField(verbose_name=u"试卷总分", default=0)# 试卷总分

    class Meta:
        verbose_name = u"用户总分管理"
        verbose_name_plural = verbose_name

    def __repr__(self):
        return "({0}) 试卷:{1} | 总分:{2}".format(self.user.username,self.paper.name,self.total)

    __str__ = __repr__


class Sign(models.Model):
    grade = models.ForeignKey(Grade,verbose_name="班级")
    sign_name = models.CharField(verbose_name="名称",max_length=20,default="")
    add_time = models.DateTimeField(verbose_name=u"创建时间", default=datetime.now)
    time_length = models.IntegerField(verbose_name="签到时间范围",default=5)

    class Meta:
        verbose_name = u"签到管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.sign_name

    __repr__ = __str__

    def wait(self):
        return self.add_time >= datetime.now()-timedelta(minutes=float(self.time_length))


class SignUser(models.Model):
    sign = models.ForeignKey(Sign,verbose_name="签到")
    students = models.ForeignKey(UserProfile,verbose_name="学生")
    create_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "签到与用户管理"
        verbose_name_plural = verbose_name
